<template>
  <div class="mask">
    <div class="inner">
        <ul>
            <li @click="isSelect(1)" :class="{active : liActiveClass == 1 }">修改基本信息</li>
            <li @click="isSelect(2)" :class="{active : liActiveClass == 2 }">修改密码</li>
            <li @click="isSelect(3)" :class="{active : liActiveClass == 3 }">修改头像</li>
        </ul>
        <div class="box" v-show="liActiveClass == 1">
            <el-form :label-position="labelPosition" label-width="80px" :model="info">
                <el-form-item label="姓名">
                    <el-input v-model="info.name" style="width: 220px;"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="info.sex">
                        <el-radio :label=1>男</el-radio>
                        <el-radio :label=0>女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="Email">
                    <el-input v-model="info.email" style="width: 220px;"></el-input>
                </el-form-item>
                <el-form-item label="钓龄">
                    <el-input v-model="info.fishage" style="width: 220px;"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-cascader size="large" :options="pcaTextArr" v-model="info.address"></el-cascader>
                </el-form-item>
            </el-form>
                <div class="btn-group">
                <el-button class="btn" @click="handleSubmit">提交</el-button>
                <el-button class="btn" @click="handleCancel">取消</el-button>
                </div>
        </div>
        <div class="box" v-show="liActiveClass == 2" style="margin-top: 100px;">
            <el-form ref="form" :model="password" label-width="80px">
                <el-form-item label="旧密码" style="width: 320px;">
                    <el-input  type="password" v-model="password.oldpassword" placeholder="请输入您的旧密码"></el-input>
                </el-form-item>
                <el-form-item label="新密码" style="width: 320px;">
                    <el-input  type="password" v-model="password.newpassword" placeholder="请输入您的新密码"></el-input>
                </el-form-item>
            </el-form>
            <div class="btn-group">
                <el-button class="btn" @click="handleChangePwd">提交</el-button>
                <el-button class="btn" @click="handleCancel">取消</el-button>
            </div>
        </div>
        <div class="box avatar-box" v-show="liActiveClass == 3">
            <el-upload
                class="avatar-uploader"
                action="#"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :on-error="handleAvatarError"
                :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </div>
    </div>
  </div>
</template>

<script>
import { mapState,mapMutations ,mapActions} from 'vuex';
import {pcaTextArr} from "element-china-area-data";
import {ChangeUserDetail,ChangePwd,ChangeAvatar,UserInfo} from '../request/api'
import _ from 'lodash';
import  Vue from 'vue';
export default {
  data(){
    return{
        liActiveClass:1,
        labelPosition:'right',
        pcaTextArr,
        password:{
            oldpassword:'',
            newpassword:''
        },
        imageUrl: '',
        
    }
},
  computed:{
      ...mapState({
        userDetail:(state) => state.userInfo.userDetail
      }),
      info(){
        // 深拷贝，变成响应式数据
        const deepInfo = _.cloneDeep(this.userDetail)
        const reactiveInfo = Vue.observable(deepInfo);
        return reactiveInfo
      }
  },
  methods:{
    ...mapMutations({
        setUserDetail:'userInfo/setUserDetail',
    }),
    ...mapActions({
        asyncChangeIsShowToast:'toastStatus/asyncChangeIsShowToast'
    }),
    // 修改页面的切换
      isSelect(index){
          this.liActiveClass = index
      },
    //   取消按钮
      handleCancel(){
        this.$emit('changeIsShow',false)
      },
    //   基本信息的提交
    async handleSubmit(){
        // console.log("!!!",this.info)
        this.info.address = this.info.address + ''
        this.info.sex = parseInt(this.info.sex)
        this.info.fishage = parseInt(this.info.fishage)
        this.info.regdate = '2022-01-01'
        this.info.avator = this.userDetail.avator
        this.info.backimg = this.userDetail.backimg
        // 提交到后端
        let res =await ChangeUserDetail(this.info)
        if(res.data.state == 200){
            // 修改vuex中的数据
            this.setUserDetail(this.info)
            // 提示成功
            this.asyncChangeIsShowToast({
                msg:'修改成功',
                type:'success',
            })
        }else{
            this.asyncChangeIsShowToast({
                msg:'修改失败',
                type:'error',
            })
        }
        // 关闭弹窗
        this.handleCancel()
    },
    // 修改密码的提交
    async handleChangePwd(){
        // 发送请求
        let res = await ChangePwd(this.password)
        // console.log("res",res.data.state)
        if(res.data.state == 200){
            // 提示成功  type="password"
            this.asyncChangeIsShowToast({
                msg:'修改成功',
                type:'success',
            })
            // 关闭弹窗
            this.handleCancel()
        }else{
            // 修改失败
            this.asyncChangeIsShowToast({
             msg:"修改失败",
             type:'error'   
            })
            // 关闭弹窗
            this.handleCancel()
        }
        
    },
    // 修改头像
    handleAvatarSuccess(res, file) {
        // console.log("file.raw",file)
        // this.imageUrl = URL.createObjectURL(file.raw);
    },
    async beforeAvatarUpload(file) {
        const formData = new FormData();
        formData.append('file', file);
        // 发送请求
        let res = await ChangeAvatar(formData)
        // console.log("other.vue error")
        // 如果成功
        if(res.data.state == 200){
            this.getUserDetails()
        // 提示
            this.asyncChangeIsShowToast({
                msg:'修改成功',
                type:'success'
                })
        }else{
            // 提示
        this.asyncChangeIsShowToast({
            msg:'修改失败',
            type:'error'
            })
        }
    },
    handleAvatarError(err){
        console.log("!!!",err)
    },
    // 重置userInfoDetail
    async getUserDetails(){
        let res = await UserInfo()
        if(res.data.state == 200){
            // 查询成功
            this.setUserDetail(res.data.data)
        }else{
            console.log("失败了")
        }
    },
    mounted() {
        //this.info = this.userDetail
        // console.log("info1",this.info1)
    },
  }
}
</script>

<style scoped>
.mask{
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0;
    left: 0;
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
}
.btn-group{
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
}

.inner{
    width: 500px;
    height: 500px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: white;
    border-radius: 10px;
}
.avatar-uploader{
    width: 180px;
    margin: 0 auto;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  /* .avatar-uploader .el-upload:hover {
    /* border-color: #409EFF; 
  } */
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
ul{
    width: 80%;
    margin: 0 auto;
    list-style: none;
    height: 30px;
    line-height: 30px;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    margin-top: 40px;
    font-size: 18px;
}
li{
    cursor: pointer;
}
.active{
    color: #0094ff;
    border-bottom: 2px solid #0094ff; 
 }

 .box{
    width: 70%;
    /* border: 1px solid #0094ff; */
    margin: 0 auto;
    margin-top: 50px;
 }
</style>
